let _viewer
import DeviceSpjkPopup from '@/views/new-screen/components/device-spjk-popup.vue'
import DeviceDzwlPopup from '@/views/new-screen/components/device-dzwl-popup.vue'
import WarningDzwlPopup from '@/views/new-screen/components/warning-dzwl-popup.vue'
import WarningSpjkPopup from '@/views/new-screen/components/warning-spjk-popup.vue'
import { usePopup } from '@/hooks/usePopup'
import mitter from '@/utils/eventBus'

const { setPopup, getPopup, createPopup } = usePopup()

export const billboardClickEvent = (movement) => {
  const pickedObject = _viewer.scene.pick(movement.position)
  // 判断是否选中了一个实体
  if (
    Cesium.defined(pickedObject) &&
    pickedObject.id instanceof Cesium.Entity
  ) {
    const entity = pickedObject.id
    if (entity.properties && entity.properties.img) {
      // console.log('点击了实体', entity)
      const cartographic = Cesium.Cartographic.fromCartesian(
        entity.position.getValue(),
      )
      const longitude = Cesium.Math.toDegrees(cartographic.longitude)
      const latitude = Cesium.Math.toDegrees(cartographic.latitude)
      _viewer.camera.flyTo({
        destination: new window.Cesium.Cartesian3.fromDegrees(
          longitude,
          latitude,
          3000,
        ),
        orientation: {
          heading: window.Cesium.Math.toRadians(0),
          pitch: window.Cesium.Math.toRadians(-90),
          roll: 0,
        },
      })
      mitter.emit('clickBillboard', entity.properties.label?.getValue() || '')
      // 取消弹出框逻辑
      return
      const popup = getPopup('spjk')
      if (popup) {
        popup.visible ? popup.hide() : popup.show()
      } else {
        const popup = createPopup(DeviceSpjkPopup, null, {
          id: 'spjk-popup',
        })
        setPopup('spjk', popup)
        // const popup = createPopup(DeviceDzwlPopup, null, {
        //   id: 'dzwl-popup'
        // })
        // setPopup('dzwl', popup)
        // const popup = createPopup(WarningSpjkPopup, null, {
        //   id: 'warning-spjk-popup'
        // })
        // setPopup('warning-spjk', popup)
        // const popup = createPopup(WarningDzwlPopup, null, {
        //   id: 'warning-dzwl-popup'
        // })
        // setPopup('warning-dzwl', popup)
      }
    }
  }
}

export const addBillboardClickEvent = (viewer, handler) => {
  _viewer = viewer
  handler.setInputAction(
    billboardClickEvent,
    Cesium.ScreenSpaceEventType.LEFT_CLICK,
  )
}
